package cool.webstudy.admin.utils;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 莫振双
 * @date 2025/1/4 18:41
 * @description: IP工具
 */
public class IPUtil {
    /**
     * @param startIP 起始ip
     * @param endIP   截止ip
     * @param isContainEndIP 是否包含截止ip
     * @return List<String> ip地址列表
     * @description: 生成ip地址列表
     */
    public static List<String> generateIpList(String startIP, String endIP,Boolean isContainEndIP) {
        try {
            InetAddress startInetAddress = InetAddress.getByName(startIP);
            byte[] startBytes = startInetAddress.getAddress();
            List<String> ipList = new ArrayList<>();
            for (int i = 0; i < startBytes.length; i++) {
                for (int j = Integer.parseInt(startIP.split("\\.")[i]); j < Integer.parseInt(endIP.split("\\.")[i]); j++) {
                    byte[] tempBytes = startBytes.clone();
                    tempBytes[i] = (byte) j;
                    ipList.add(InetAddress.getByAddress(tempBytes).getHostAddress());
                }
            }
            if (isContainEndIP) {
                ipList.add(endIP);
            }
            return ipList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
